<script setup lang="ts">
import { ref, onMounted } from "vue";
import { useRouter, useRoute } from "vue-router";
import { getToken } from "@/utils/auth";
import { queryChildByTypeCodeAndItemValue } from "@/api/machineClass";

const router = useRouter();
const route = useRoute();
const accessToken = getToken().accessToken || "";
const ruleCode = ref("");
const eqpGroupId = ref("");
const url = ref("");
const facCode = ref<any>("");

window.addEventListener("message", function (e) {
  if (e.data) {
    try {
      const res = JSON.parse(e.data);
      if (res?.code === "JobList") {
        url.value = `${import.meta.env.VITE_API_URL_PMS_NEW}/#/pmJob/list?ssoToken=${accessToken}&isHiddenMenu=true&facCode=${facCode.value}&className=MACHINE`;
      }
      if (res?.code === "PlanList") {
        url.value = `${import.meta.env.VITE_API_URL_PMS_NEW}/#/pmPlan/list?pmType=UNS&isAdd=true&ssoToken=${accessToken}&isHiddenMenu=true&facCode=${facCode.value}&ruleCode=${ruleCode.value}&className=MACHINE`;
        // router.push({
        //   path: "/all/equip/epm/plan/list",
        //   query: {
        //     fullpath: `/pmPlan/list?pmType=UNS&isAdd=true&ssoToken=${accessToken}&isHiddenMenu=true&facCode=${facCode.value}&className=MACHINE&systemCode=`
        //   }
        // });
      }
    } catch (e) {}
  }
});

// router.push({
//   path: "/all/equip/epm/plan/list",
//   query: {
//     fullpath: `/pmPlan/list?pmType=UNS&isAdd=true&ssoToken=${accessToken}&isHiddenMenu=true&facCode=${厂区code}&className=${科室code}&systemCode=${系统code}${设备总称id}`
//   }
// });

const permission = localStorage.getItem("pmsPermission");

const onLoad = () => {
  const iframePlan = document.getElementById("PMS_IFRAME_PLAN") as HTMLIFrameElement;
  iframePlan?.contentWindow?.postMessage(JSON.parse(permission), "*");
};

onMounted(async () => {
  // 通过厂区字典查询区域
  const codes: any = await queryChildByTypeCodeAndItemValue({
    value: "CR_ALL_CODES",
    typeCode: "MCTS"
  });
  facCode.value = JSON.parse(codes.filter(item => item.itemValue === "CR_POSITION_CODE")[0].itemDesc)[0].facCode; // 暂时写死一期厂区
  const resData: any = await queryChildByTypeCodeAndItemValue({
    value: "EQP_CONSUMABLE",
    typeCode: "CONSUMABLE_LIFE_CYCLE"
  });
  ruleCode.value = JSON.parse(resData.filter(el => el.itemValue === "EQP_FFU")[0].itemDesc.replace(/[\r|\n|\t]/g, "")).RULE_CODE;
  eqpGroupId.value = JSON.parse(resData.filter(el => el.itemValue === "EQP_FFU")[0].itemDesc.replace(/[\r|\n|\t]/g, "")).EQP_TYPE_ID;
  if (route.query.isAdd === "ADD" && route.query.categoryId && route.query.systemCode) {
    url.value = `${import.meta.env.VITE_API_URL_PMS_NEW}/#/pmPlan/list?pmType=UNS&isAdd=true&ssoToken=${accessToken}&isHiddenMenu=true&facCode=${facCode.value}&ruleCode=${ruleCode.value}&className=MACHINE&categoryId=${route.query.categoryId}&systemCode=${route.query.systemCode}`;
  } else {
    url.value = `${import.meta.env.VITE_API_URL_PMS_NEW}/#/pmJob/list?ssoToken=${accessToken}&isHiddenMenu=true&facCode=${facCode.value}&className=MACHINE`;
  }
});
</script>

<template>
  <div class="empty_page">
    <iframe :src="url" width="100%" height="100%" frameborder="0" @load="onLoad" />
  </div>
</template>

<style lang="scss" scoped>
.empty_page {
  width: 100% !important;
  height: 100% !important;
  margin: 0 !important;
}
</style>
